package com.quality.web.controller.business;

import com.quality.business.domain.QualityRule;
import com.quality.business.domain.QualityStatistics;
import com.quality.business.domain.resp.StatisticsResp;
import com.quality.business.service.QualityRuleService;
import com.quality.business.service.QualityStatisticsService;
import com.quality.common.core.controller.BaseController;
import com.quality.common.core.domain.entity.SysUser;
import com.quality.common.core.domain.model.LoginUser;
import com.quality.common.core.page.TableDataInfo;
import com.quality.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hjh
 * @Description TODO
 * @createTime 2022年06月30日 17:16:00
 */
@PreAuthorize("@ss.hasAnyRoles('common_admin,admin')")
@RestController
@RequestMapping("/quality/statistics")
public class QualityStatisticsController extends BaseController {

    @Autowired
    private QualityStatisticsService statisticsService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private QualityRuleService ruleService;

    @GetMapping("/list")
    public TableDataInfo list(){
        startPage();
        List<QualityStatistics> list = statisticsService.list();
        List<StatisticsResp> resps = new ArrayList<>();
        for (QualityStatistics s : list) {
            StatisticsResp resp = new StatisticsResp();
            SysUser sysUser = userService.selectUserById(s.getUserId());
            QualityRule rule = ruleService.getById(s.getRuleId());
            resp.setUserId(s.getUserId()).setUsername(sysUser.getUserName())
                    .setDescription(rule.getDescription()).setTable(rule.getTable())
                    .setQualified(s.getQualified()).setFailed(s.getFailed())
                    .setNum(s.getNum()).setId(s.getId());
            Integer integer = rule.getRule();
            switch (integer){
                case 1:
                    resp.setRule("非空字符串");
                    break;
                case 2:
                    resp.setRule("数据范围检查");
                    break;
                case 3:
                    resp.setRule("日期范围检查");
                    break;
                case 4:
                    resp.setRule("正则表达式");
                    break;
            }
            resps.add(resp);
        }
        TableDataInfo dataTable = getDataTable(resps);
        dataTable.setTotal(getDataTable(list).getTotal());
        return dataTable;
    }

}
